package com.cysyz.mylib.excel.engine.impl.jxl;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import jxl.Workbook;
import jxl.read.biff.BiffException;

import com.cysyz.mylib.excel.engine.ExcelEngine;
import com.cysyz.mylib.excel.engine.SheetEngine;
import com.cysyz.mylib.excel.exception.ExcelException;

public class ExcelEngineJxlImpl implements ExcelEngine {
	Workbook book =null;
	
	public ExcelEngineJxlImpl(File excelFile) throws ExcelException {
		
		List<String> errorList = new ArrayList<String>();
		try {
			book = Workbook.getWorkbook(excelFile);
			book.getNumberOfSheets();
		} catch (BiffException e) {
			errorList.add("无法识别的文件格式");
			throw new ExcelException(errorList);
		} catch (IOException e) {
			errorList.add("文件传输过程中发生意外，请重试");
			throw new ExcelException(errorList);
		} catch (IndexOutOfBoundsException e) {
			errorList.add("文件中没有sheet");
			throw new ExcelException(errorList);
		}
	}

	@Override
	public SheetEngine getSheet(String sheetName) {
		return new SheetEngineJxlImpl(book.getSheet(sheetName));
	}

	@Override
	public SheetEngine getSheet(int sheetIdx) {
		return new SheetEngineJxlImpl(book.getSheet(sheetIdx));
	}

	@Override
	public int getNumberOfSheets() {
		return book.getNumberOfSheets();
	}

}
